if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2500, 3500);

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function 公告() {
  var close_btn = text('javascript:;').findOne(20000)
  if (close_btn) {
    sleep(time)
    toastLog('发现公告弹窗')
    sleep(time)
    close_btn.click()
  } else {
    toastLog('没有公告弹窗，继续做任务')
  }
  utils.updateAppProgress(20);
}

function task() {
  var 签到 = text('签到').findOne(5000)
  if (签到) {
    sleep(5000)
    签到.click()
    sleep(time)
  }
  var 查看我的云池 = text('查看我的云池').findOne(5000) || text('查看我的云池').findOne(5000)
  if (!查看我的云池) {
    toastLog("查看我的云池按钮未找到.")
    return ;
  }
  click(查看我的云池.bounds().centerX(),查看我的云池.bounds().centerY())
  sleep(time)
  var 一键提取云池收益 = text('一键提取云池收益').findOne(5000)
  if (!一键提取云池收益) {
    toastLog("一键提取云池收益按钮未找到.")
    return ;
  }
  sleep(time)
  click(一键提取云池收益.bounds().centerX(),一键提取云池收益.bounds().centerY())

}
 

function main() {
  let open_app = launchAppEnhance("DTS");
  if (open_app) {
    公告()
    task()
  }
  else {
    toastLog("DTS打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");